package com.niiwoo.civet.mobile.lanmao.handler;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.niiwoo.civet.account.dto.request.UpdateRechargeRequestDTO;
import com.niiwoo.civet.account.dto.response.UpdateRechargeResponseDTO;
import com.niiwoo.civet.account.service.recharge.RechargeDubboService;
import com.niiwoo.tripod.lanmao.enums.LanMaoDirectServiceNameEnum;
import com.niiwoo.tripod.lanmao.enums.LanMaoGatewayServiceNameEnum;
import com.niiwoo.tripod.lanmao.enums.ResponseCodeEnum;
import com.niiwoo.tripod.lanmao.enums.ResponseStatusEnum;
import com.niiwoo.tripod.lanmao.response.RechargeResponse;
import com.niiwoo.tripod.provider.exception.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Created by zhangwanli on 2017/11/9.
 */
@Component
public class RechargeNotifyHandler implements LanMaoNotifyHandler {
    public static final Logger logger = LoggerFactory.getLogger(RechargeNotifyHandler.class);
    @Reference(version = "1.0.0")
    private RechargeDubboService rechargeDubboService;

    @Override
    public boolean supports(String serviceName) {
        return LanMaoGatewayServiceNameEnum.RECHARGE.name().equals(serviceName) || LanMaoDirectServiceNameEnum.DIRECT_RECHARGE.name().equals(serviceName);
    }

    @Override
    public HandleResult handle(JSONObject json) {
        HandleResult result = new HandleResult();
        logger.info("充值回调处理###报文###data={}", json.toJSONString());
        RechargeResponse rechargeResponse = json.toJavaObject(RechargeResponse.class);
        logger.info("充值报文转换#####rechargeResponse={}", JSON.toJSONString(rechargeResponse));
        try {
            UpdateRechargeRequestDTO rechargeRequestDTO = new UpdateRechargeRequestDTO();
            rechargeRequestDTO.setRequestNo(rechargeResponse.getRequestNo());
            rechargeRequestDTO.setAmount(rechargeResponse.getAmount());
            rechargeRequestDTO.setResponseCodeEnum(String.valueOf(rechargeResponse.getCode()));
            rechargeRequestDTO.setRechargeStatusEnum(rechargeResponse.getRechargeStatus());
            rechargeRequestDTO.setTransactionTime(rechargeResponse.getTransactionTime());
            rechargeRequestDTO.setAccountNo(rechargeResponse.getPlatformUserNo());
            rechargeRequestDTO.setRechargeWayEnum(rechargeResponse.getRechargeWay());
            rechargeRequestDTO.setErrorCode(rechargeResponse.getErrorCode());
            rechargeRequestDTO.setErrorMessage(rechargeResponse.getErrorMessage());
            rechargeRequestDTO.setChannelErrorCode(rechargeResponse.getChannelErrorCode());
            rechargeRequestDTO.setChannelErrorMessage(rechargeResponse.getChannelErrorMessage());
            logger.info("充值回调处理###请求###request={}", JSON.toJSONString(rechargeRequestDTO));
            Byte code = rechargeResponse.getCode();
            String status = rechargeResponse.getStatus();
            //如果调用成功
            if (ResponseCodeEnum.SUCCESS.getValue().equals(code) && ResponseStatusEnum.SUCCESS.getValue().equals(status)) {
                logger.info("充值回调处理成功#####code={}", code);
                UpdateRechargeResponseDTO updateRechargeResponseDTO = rechargeDubboService.updateRecharge(rechargeRequestDTO);
                logger.info("充值更新订单成功#####updateRechargeResponseDTO={}", JSON.toJSONString(updateRechargeResponseDTO));
                if (updateRechargeResponseDTO != null) {
                    result.setNeedAck(true);
                    result.setData(updateRechargeResponseDTO);
                    result.setRequestNo(rechargeResponse.getRequestNo());
                    return result;
                }
            } else {
                logger.info("充值回调处理失败#####code={}", code);
            }
        } catch (BizException e) {
            logger.error("充值回调处理###异常###errorCode={}, errorMessage={}", e.getErrorCode(), e.getMessage());
            throw e;
        } catch (Exception e) {
            logger.error(String.format("充值回调处理###未知异常###errorMessage=%s", e.getMessage()), e);
            throw new BizException("SYS9999");
        }
        return result;
    }

}
